package io.cici.cc.mybatis.lite.reflection;

import java.util.Iterator;


public class PropertyTokenizer implements Iterator<PropertyTokenizer> {
    private final String nameAndIndex;
    private final String next;
    private String name;
    private String index;

    public PropertyTokenizer(String fullName) {
        int indexOf = fullName.indexOf('.');
        if (indexOf > -1) {
            name = fullName.substring(0, indexOf);
            next = fullName.substring(indexOf + 1);
        } else {
            name = fullName;
            next = null;
        }
        nameAndIndex = name;
        indexOf = name.indexOf('[');
        if (indexOf > -1) {
            index = name.substring(indexOf + 1, name.length() - 1);
            name = name.substring(0, indexOf);
        }
    }

    public String getNameAndIndex() {
        return nameAndIndex;
    }

    public String getName() {
        return name;
    }

    public String getIndex() {
        return index;
    }


    public String getNext() {
        return next;
    }

    @Override
    public boolean hasNext() {
        return next != null;
    }

    @Override
    public PropertyTokenizer next() {
        return new PropertyTokenizer(next);
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException(
                "Remove is not supported, as it has no meaning in the context of properties.");
    }
}
